package com.bi.onlineEducation.service;

import com.bi.onlineEducation.dao.CourseDao;
import com.bi.onlineEducation.dao.CoursePathDao;
import com.bi.onlineEducation.domain.CoursePathPo;
import com.bi.onlineEducation.domain.CoursePo;
import com.bi.onlineEducation.utils.FtpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: chenYuTao
 * @Time: 2022/1/19 23:41
 * @Description:
 **/
@Slf4j
@Service
public class CourseService {
    @Resource
    private CourseDao courseDao;
    @Resource
    private CoursePathDao coursePathDao;
    @Resource
    private FtpUtil ftpUtil;

    public List<CoursePo> findAll() {
        return courseDao.findAll();
    }

    public CoursePo findById(Integer id) {
        return courseDao.findById(id).get();
    }

    public List<CoursePo> findAllByLId(Integer id) {
        return courseDao.findAllBylId(id);
    }

    public CoursePo save(CoursePo coursePo) {
        return courseDao.save(coursePo);
    }

    @Transactional
    public Boolean deleteById(Integer id) {
        CoursePo coursePo = Optional.ofNullable(courseDao.findById(id)).map(vo -> vo.get()).orElseThrow();
        String fileName = coursePo.getWatchPath().split("/")[coursePo.getWatchPath().split("/").length - 1];
        ftpUtil.deleteFile(fileName);
        log.info("已删除文件名={}", fileName);
        CoursePathPo coursePathPoBycId = coursePathDao.findCoursePathPoBycId(id);
        if (ObjectUtils.isNotEmpty(coursePathPoBycId)) {
            coursePathDao.delete(coursePathPoBycId);
        }
        courseDao.deleteById(id);
        return true;
    }

    public Boolean deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
        return true;
    }

    /**
     * 订单搜索所有
     *
     * @param cName
     * @param cStatus
     * @return
     */
    public List<CoursePo> search(String cName, Integer cStatus) {
        return getSearchPurchaseList(cName, cStatus);
    }

    /**
     * 包含当前用户订单搜索
     *
     * @param cName
     * @param cStatus
     * @param lId
     * @return
     */
    public List<CoursePo> searchBylId(String cName, Integer cStatus, Integer lId) {
        ArrayList<CoursePo> resultList = new ArrayList<>();
        for (CoursePo coursePo : getSearchPurchaseList(cName, cStatus)) {
            if (coursePo.getLogin().getLId() != lId.intValue()) continue;
            resultList.add(coursePo);
        }
        return resultList;
    }


    private ArrayList<CoursePo> getSearchPurchaseList(String cName, Integer cStatus) {
        List<CoursePo> all = courseDao.findAll();
        ArrayList<CoursePo> resultList = new ArrayList<>();
        ArrayList<CoursePo> cNameList = new ArrayList<>();
        ArrayList<CoursePo> cStatusList = new ArrayList<>();
        for (CoursePo coursePo : all) {
            if (StringUtils.isNotBlank(cName) && coursePo.getCName().indexOf(cName) > -1) {
                cNameList.add(coursePo);
            }
            if (cStatus != null && coursePo.getCStatus() == (cStatus.intValue() == 1 ? true : false)) {
                cStatusList.add(coursePo);
            }
        }
        if (StringUtils.isBlank(cName)) cNameList = (ArrayList<CoursePo>) all;
        if (cStatus == null || cStatus == 2) cStatusList = (ArrayList<CoursePo>) all;
        for (CoursePo realNameVo : cNameList) {
            for (CoursePo addressVo : cStatusList) {
                if (realNameVo.getCId() != addressVo.getCId()) continue;
                resultList.add(realNameVo);
            }
        }
        return resultList;
    }
}
